package com.duowei.diandan.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.activity.WelcomeActivity;
import com.duowei.diandan.app.InitManager;
import com.duowei.diandan.databinding.DataCommitFragmentBinding;
import com.duowei.diandan.event.CountdownEvent;
import com.duowei.diandan.sql.DataCommit;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

public class DataCommitFragment extends BaseFragment implements DataCommit.OnCommitListener {
    public static final String TAG = "DataCommitFragment";

    private DataCommitFragmentBinding mBinding;

    private DataCommit mDataCommit;

    private boolean mRemoteCommit;

    /**
     * @param remoteCommit true,付款; false, 只提交数据
     */
    public static DataCommitFragment newInstance(boolean remoteCommit) {

        Bundle args = new Bundle();
        args.putBoolean("remoteCommit", remoteCommit);

        DataCommitFragment fragment = new DataCommitFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mRemoteCommit = getArguments().getBoolean("remoteCommit");
        }
        mDataCommit = new DataCommit(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data_commit, container, false);

        mBinding.continueButton.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), WelcomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        });

        return mBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        mBinding.dilatingDotsProgressBar.showNow();

        mDataCommit.dataCommit(mRemoteCommit);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
        mBinding.dilatingDotsProgressBar.hide();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDataCommit.stopCommit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCountdownEvent(CountdownEvent e) {
        if (mBinding.paySuccessLayout.getVisibility() == View.VISIBLE) {
            mBinding.continueButton.setText(String.format(Locale.CHINA, "返回(%ds)", e.second));
        }
    }

    @Override
    public void onSuccess() {
        InitManager.getInstance().setMaxCountdownSecond(15);

        mBinding.paySuccessLayout.setVisibility(View.VISIBLE);
        mBinding.commitLayout.setVisibility(View.GONE);
    }

    @Override
    public void onPlateNotSet() {
        showToast("没发现餐牌设置", false);

        if (getFragmentManager() != null) {
            getFragmentManager().popBackStack();
        }
    }
}
